package com.lannuokeji.biz.ac.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lannuokeji.base.AController;
import com.lannuokeji.biz.ac.entity.User;
import com.lannuokeji.biz.ac.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-17
 */
@Api(value="用户", description="用户个人操作")
@RestController
@RequestMapping("/ac/user")
public class UserController extends AController {

	@Resource
	UserService userService;
	
	@ApiOperation(value = "用户退出登录")
	@PostMapping("/logout")
	public Object logout(HttpServletRequest request) {
		SecurityUtils.getSubject().logout();
		return setSuccessContent("成功");
	}
	
	@ApiOperation(value = "查看个人基本信息")
	@GetMapping("/selfInfo")
	public Object getUserInfo(HttpServletRequest request) {
		String id = getUser();
		User user = userService.getUserInfo(id);
		return setSuccessContent(user);
	}

	@ApiOperation(value = "修改个人基本信息")
	@PutMapping("/selfInfo")
	public Object updateUserInfo(HttpServletRequest request,
			@ApiParam(name = "email", value = "邮箱", required = true) @RequestParam("email") String email,
			@ApiParam(name = "phone", value = "手机号", required = true) @RequestParam("phone") String phone) {
		String userId = getUser();
		if (userService.updateUserInfo(userId, email, phone)) {
			return setSuccessContent("修改成功");
		} else {
			return setErrorContent("修改失败");
		}
	}


	@ApiOperation(value = "修改密码")
	@PutMapping("/password")
	public Object updatePassword(HttpServletRequest request,
			@ApiParam(name = "password", value = "原密码", required = true) @RequestParam String password,
			@ApiParam(name = "repassword", value = "新密码", required = true) @RequestParam("repassword") String repassword) {
		HttpSession session = request.getSession();
		String id = session.getAttribute("id").toString();
		if (userService.updatePassword(id, password, repassword)) {
			return setSuccessContent("修改成功");
		} else {
			return setErrorContent("修改失败");
		}
	}

}

